package com.example.zwlyz.ui.settings;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.zwlyz.AddArticleActivity;
import com.example.zwlyz.AddProductActivity;
import com.example.zwlyz.R;
import com.example.zwlyz.AccountManagementActivity;
import com.example.zwlyz.LoginActivity;

public class SettingsFragment extends Fragment {

    private Switch adminModeSwitch;
    private Button btnAccountManagement;
    private Button btnLogout;
    private Button btnAddArticle;
    private Button btnAddProduct;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);

        adminModeSwitch = view.findViewById(R.id.adminModeSwitch);
        btnAccountManagement = view.findViewById(R.id.btnAccountManagement);
        btnLogout = view.findViewById(R.id.btnLogout);
        btnAddArticle = view.findViewById(R.id.btnAddArticle);
        btnAddProduct = view.findViewById(R.id.btnAddProduct);

        adminModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            btnAddArticle.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            btnAddProduct.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        });

        btnAccountManagement.setOnClickListener(v -> {
            // 跳转到账号管理页面
            startActivity(new Intent(getContext(), AccountManagementActivity.class));
        });

        btnLogout.setOnClickListener(v -> {
            // 退出登录
            startActivity(new Intent(getContext(), LoginActivity.class));
            requireActivity().finish();
        });

        btnAddArticle.setOnClickListener(v -> {
            // 跳转到添加文章页面
            startActivity(new Intent(getContext(), AddArticleActivity.class));
        });

        btnAddProduct.setOnClickListener(v -> {
            // 跳转到添加商品页面
            startActivity(new Intent(getContext(), AddProductActivity.class));
        });

        return view;
    }
}